// pages/UpdateDeliveryAddress/UpdateDeliveryAddress.js
import {
  queryDeliveryAddressById,
  modifyDeliveryAddress
} from "../../service/delivery-address"

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  /**
   * 点击选择省市区按钮
   */
  onTapSelectCityButton() {
    const self = this
    wx.navigateTo({
      url: '/pages/UpdateDeliveryAddress/ProvinceCityDistrictSelection/ProvinceCityDistrictSelection',
      events: {
        acceptRegionData(data) {
          console.log(data);
          self.setData({
            province: data.province,
            city: data.city,
            district: data.district
          })
        }
      }
    })
  },

  /**
   * 点击保存收货地址按钮
   */
  async onTapSaveButton() {
    console.log(this.data);
    const param = {}
    param.receiverName = this.data.receiverName
    param.receiverPhone = this.data.receiverPhone
    param.province = this.data.province
    param.city = this.data.city
    param.district = this.data.district
    param.detailAddress = this.data.detailAddress
    param.isDefault = this.data.isDefault
    await modifyDeliveryAddress(this.data.id, param)
    const channel = this.getOpenerEventChannel()
    channel.emit("update")
    wx.navigateBack()
  },
  /**
   * 点击更改默认地址选择状态 
   */
  onChange() {
    this.setData({
      isDefault: this.data.isDefault === 1 ? 0 : 1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const addressId = Number(options.addressId)
    const res = await queryDeliveryAddressById(addressId)
    this.setData({
      ...res.data
    })
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})